//
//  Mine_RootViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2021/12/10.
//

#import "Mine_RootViewController.h"
#import "AboutViewController.h"
#import "SetTableViewCell.h"
#import "Set_Root_TableViewHeaderView.h"

#import "OpinionViewController.h"
#import "UserLogoutApi.h"
#import "HeartInformationListViewController.h"
#import "CollectGameListViewController.h"

#import "MineViewModel.h"

#define WHITEHEIGHT 100.00//背景色渐变为白色时的高度

@interface Mine_RootViewController ()

@property (nonatomic, strong) UILabel *leftTitle;

@property (nonatomic, strong) Set_Root_TableViewHeaderView *headView;
@property (nonatomic, strong) UIButton *loginoutBtn;

@property (nonatomic, strong) NSArray *cellDatas;
@property (nonatomic, strong) MineViewModel *viewModel;

@end

@implementation Mine_RootViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:NO];

    [self refreshAfterLogin];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)setupUI {
    [super setupUI];
    
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 50;
    self.tableView.rowHeight = 50;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SetTableViewCell" bundle:nil] forCellReuseIdentifier:@"SetTableViewCell"];
    self.headView = [[NSBundle mainBundle] loadNibNamed:@"Set_Root_TableViewHeaderView" owner:self options:nil].firstObject;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, 40)];
    VHWeakSelf
    self.headView.headerClickClock = ^(MineSubViewType sign) {
        VHStrongSelf
        [self headerClickBlock:sign];
    };
    
    self.tableView.frame = CGRectMake(0, 0, SL_WINDOW_WIDTH, SL_WINDOW_HEIGHT - UIDevice.vg_tabBarFullHeight);
    self.headView.frame = CGRectMake(0, 0, SL_WINDOW_WIDTH, 64+80+40+(70.0/172.0*(SL_WINDOW_WIDTH-30)/2.0)+15+80);
    
    self.tableView.tableHeaderView = self.headView;
    
    
}
- (void)setupNavi {
    [super setupNavi];
    UILabel *leftTitle = [[UILabel alloc] initWithFrame:CGRectMake(14, UIDevice.vg_statusBarHeight, 100, 40)];
    leftTitle.text = @"我的";
    leftTitle.font = [UIFont systemFontOfSize:18];
    leftTitle.textColor = kColor333333;
    leftTitle.hidden = YES;
    [self.naviView addSubview:leftTitle];
    self.leftTitle = leftTitle;
    NSArray *btnImages = @[@"ic_service", @"my_self_message", @"icon_setting_gray"];
    for (int i = 0; i < btnImages.count; i++) {
        UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(SL_WINDOW_WIDTH-14-40*(btnImages.count-i), UIDevice.vg_statusBarHeight, 40, 40)];
        rightBtn.tag = 100 + i;
        rightBtn.contentMode = UIViewContentModeCenter;
        [rightBtn setImage:[UIImage imageNamed_myself:btnImages[i]] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.naviView addSubview:rightBtn];
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cellDatas = [NSArray arrayWithArray:[LocalData getMenudicWithKey:@"my_self_cell"]];
    VHWeakSelf
    self.viewModel = [[MineViewModel alloc] initWithVc:self block:^(MineSubViewType type, id _Nullable obj) {
        VHStrongSelf
        [self mineBlock:type obj:obj];
    }];
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"table -- %.2f", scrollView.contentOffset.y);
    CGFloat scroll_y = scrollView.contentOffset.y;
    if (scroll_y > WHITEHEIGHT) {
        self.naviView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
        self.leftTitle.hidden = NO;
    } else {
        self.naviView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];
        self.leftTitle.hidden = YES;
    }
    
}

# pragma UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cellDatas.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (@available(iOS 15.0, *)) {
        tableView.sectionHeaderTopPadding = 0;
    }
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = self.cellDatas[section];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SetTableViewCell" forIndexPath:indexPath];
    NSArray *array = self.cellDatas[indexPath.section];
    NSDictionary *dictionary = array[indexPath.row];
    cell.imageStr = dictionary[@"img_str"];
    cell.title = dictionary[@"title_str"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MineSubViewType type = 0;
    if (indexPath.section == 0) {
        type = 12 + indexPath.row;
        [self.viewModel jumpToVC:type];
    } else if (indexPath.section == 1) {
        type = 16 + indexPath.row;
        [self.viewModel jumpToVC:type];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger sectionCount = [tableView numberOfRowsInSection:indexPath.section] - 1;// section row 个数
    CGRect bounds = CGRectInset(cell.bounds, 10, 0); // 显示的cell 点击区域
    CGFloat radius = 10;
    // 2.再盖一个 mask
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];// 用于蒙板
    // section 只有一个时。
    if (indexPath.row == 0 && indexPath.row == sectionCount) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:radius].CGPath];

        // 第一个 row
    } else if (indexPath.row == 0) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds
                                                 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
                                                       cornerRadii:CGSizeMake(radius, radius)].CGPath];

        // 最后一个 row
    } else if (indexPath.row == sectionCount) {
        [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bounds
                                                 byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
                                                       cornerRadii:CGSizeMake(radius, radius)].CGPath];
        // 中间 row
    } else {
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:bounds];
        [maskLayer setPath:path.CGPath];

    }
    // 2.mask
    [cell setMaskView:[[UIView alloc] initWithFrame:cell.bounds]];
    [cell.maskView.layer insertSublayer:maskLayer atIndex:0];
    [cell.maskView.layer setMasksToBounds:YES];
    [cell setClipsToBounds:YES];


}

- (void)refreshAfterLogin
{
    [self.tableView reloadData];
    [self.headView reloadUI];
}

- (void)headImageAction
{
    if (![LocalData fetchUserInfo]) {
        [[AppLogic sharedInstance] presentLoginVC];
        return;
    }
    [[ZCImageUpload shareInstance] addImageUploadBlock:^(UIImage * _Nonnull image) {
        NSString *imageStr = [NSObject UIImageToBase64Str:image];
        [LocalData saveHeadStr:imageStr];
        [self refreshAfterLogin];
    }];
}

- (void)rightBtnClick:(UIButton *)button
{
    [self headerClickBlock:button.tag-100];
}

///sign 标记 0客服 1消息 2设置 3登录或个人信息 4svip 5畅玩卡 6代金券 7金币 8平台币 9青铜 10富 11每日签到
- (void)headerClickBlock:(MineSubViewType)sign
{
    if (sign == MineSubViewType_kefu) {
        [self.viewModel jumpToVC:sign];
        return;
    }
    
    if (![LocalData fetchUserInfo]) {
        [[AppLogic sharedInstance] presentLoginVC];
        return;
    }
    
    [self.viewModel jumpToVC:sign];
}

- (void)mineBlock:(MineSubViewType)type obj:(id _Nullable)obj
{
    switch (type) {
        case MineSubViewType_person:
        {
            [self refreshAfterLogin];
        }
            break;
            
        default:
            break;
    }
}


@end
